package com.atues.thread.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Lock4Test {
}

class Data2{
    private Lock lock = new ReentrantLock();
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();
    private  int num = 1;
    public void printA() {
        lock.lock();
        try {
            while(num != 1){
                condition1.await();
            }
            System.out.println(Thread.currentThread().getName() + " print A");
            num = 2;//唤醒2
            condition2.signal();
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
        finally {
            lock.unlock();
        }
    }

    public void printB() {
        lock.lock();
        try {
            while(num != 2){
                condition2.await();
            }
            System.out.println(Thread.currentThread().getName() + " print C");
            num = 3;//唤醒2
            condition3.signal();
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
        finally {
            lock.unlock();
        }
    }

    public void printC() {
        lock.lock();
        try {
            while(num != 3){
                condition3.await();
            }
            System.out.println(Thread.currentThread().getName() + " print C");
            num = 1;//唤醒2
            condition1.signal();
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
        finally {
            lock.unlock();
        }
    }

}